/*
 * @(#)CorpResize.java    Jul 25, 2010
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

package com.lab.image;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Jul 25, 2010 9:22:42 AM
 */

public class ClipResize {

	enum Type {
		ZOOMIN, ZOOMOUT
	}

	private File srcFile;
	private File desFile;

	private int x;
	private int y;
	private int width;
	private int height;

	public ClipResize(File srcFile, File desFile) {
		this.srcFile = srcFile;
		this.desFile = desFile;
	}

	public BufferedImage clip(int x, int y, int width, int height)
			throws IOException {
		if (x < 0 || y < 0 || width < 0 || height < 0) {
			throw new IllegalArgumentException(
					"The X or Y coordinate or width or height of the image does not allow negative.");
		}

		FileInputStream is = null;
		ImageInputStream iis = null;

		try {
			is = new FileInputStream(srcFile);
			Iterator<ImageReader> it = ImageIO
					.getImageReadersByFormatName("jpg");
			ImageReader reader = it.next();
			iis = ImageIO.createImageInputStream(is);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();

			Rectangle rect = new Rectangle(x, y, width, height);
			param.setSourceRegion(rect);

			return reader.read(0, param);
		} finally {
			if (is != null) {
				is.close();
			}
			if (iis != null) {
				iis.close();
			}
		}
	}

	public void save(BufferedImage img) {
		try {
			ImageIO.write(img, "jpg", desFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public BufferedImage scale(double scale, Type type) {
		if (scale < 0) {
			throw new IllegalArgumentException("scale does not allow negative.");
		}

		try {
			BufferedImage srcImg = ImageIO.read(srcFile);
			int width = srcImg.getWidth();
			int height = srcImg.getHeight();

			switch (type) {
			case ZOOMIN:
				width = (int) (width * scale);
				height = (int) (height * scale);
				break;
			case ZOOMOUT:
				width = (int) (width / scale);
				height = (int) (height / scale);
				break;
			}

			Image image = srcImg.getScaledInstance(width, height,
					Image.SCALE_DEFAULT);
			BufferedImage desImg = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = desImg.getGraphics();
			g.drawImage(image, 0, 0, null);
			g.dispose();

			return desImg;
		} catch (IOException e) {
			e.printStackTrace();

			return null;
		}
	}

	public File getSrcFile() {
		return srcFile;
	}

	public void setSrcFile(File srcFile) {
		this.srcFile = srcFile;
	}

	public File getDesFile() {
		return desFile;
	}

	public void setDesFile(File desFile) {
		this.desFile = desFile;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
}
